% calculate all the feature functions
zuluWords = fopen('ZuluSegment1.txt');
C = textscan(zuluWords, '%s %s');
X = C{1};
Y = C{2};
fid = fopen('LetterWords.txt');
C = textscan(fid, '%s');
seg = C{1};
% seg = seg(1:2883);
wsize = 5; %maximum number of sequences to look at
f = cell(4*size(seg,1),2);
y_table = ['0' '1'];
tags = cell(4,1);
tags{1} = cellstr('00');
tags{2} = cellstr('01');
tags{3} = cellstr('10');
tags{4} = cellstr('11');

for i=0:3
    for iter=1:size(seg,1)
        j = iter + i*size(seg,1);
        f(j,:) = [seg(iter) tags{i+1}];
    end
end

S = struct();
for i=1:size(seg)
    S.(seg{i}) = i;
end
cross_results = zeros(10,2);
% given an input vector x, calculate out g
for cross=0:0
    letterCorr =0;
    letterTot =0;
    wordCorr =0;
    wordTot=0;
    w = ones(4*size(seg,1),1);
    step = .2;
    
    % training
    string = sprintf('training bin %d:', cross);
    disp(string);
    for ex=1:1000%size(X,1) % examples
        ex
        if(mod(ex,10)~=cross)
            g = zeros(2,2,size(X{ex},2));
            segs = zeros(4*size(seg,1),1);

            F = zeros(4*size(seg,1),1);
            usedsegs = zeros(size(seg,1),1);
            for i=2:min(wsize,size(X{ex},2))
                [F, usedsegs] = parseword( X{ex}, Y{ex}, i, S, F, usedsegs );
            end

        %     allsegs = segs(1:size(seg)) + segs((size(seg)+1):2*size(seg))
        %     + segs((2*size(seg)+1):3*size(seg)) + segs((3*size(seg)+1):4*size(seg));

            for i=1:size(X{ex},2)-1 % for each position in the example string
                % f should be a J length vector of every single feature
                % function - each entry should be ({string of chars}, {break
                % position}
                g(:,:,i) = calc_g(w,X{ex},i,f,usedsegs);
            end

            [~,~,y_max] = calc_u(g);
            y_maxstr = zeros(size(y_max'));
            for yctr=1:size(y_max,1)
                y_maxstr(yctr) = y_table(y_max(yctr));
            end

            Fy_max = zeros(4*size(seg,1),1);
            for i=2:min(wsize,size(X{ex},2))
                [Fy_max,~] = parseword( X{ex}, char(y_maxstr), i, S, Fy_max, usedsegs );
            end

            w = w + step*F;
            w = w - step*Fy_max;
        end
    end
    
%         flag = 0;
%         for k = 1:size(Y{ex},2)
%             letterTot = letterTot + 1;
%             if y_table(y_max(k)) == Y{ex}(k)
%                 letterCorr = letterCorr + 1;
%             else
%                 flag = 1;
%             end
%         end
% 
%         if flag ~= 1
%             wordCorr = wordCorr + 1;
%         end
%         wordTot = wordTot +1;
%         end
%     end


%     letterCorr/letterTot
%     wordCorr/wordTot


    % testing
    string = sprintf('testing bin %d:', cross);
    disp(string);
    for ex=1:1000%size(X,1) % examples
        if(mod(ex,10)==cross)
            g = zeros(2,2,size(X{ex},2));
            segs = zeros(4*size(seg,1),1);

            F = zeros(4*size(seg,1),1);
            usedsegs = zeros(size(seg,1),1);
            for i=2:min(wsize,size(X{ex},2))
                [F,usedsegs] = parseword( X{ex}, Y{ex}, i, S, F, usedsegs );
            end

        %     allsegs = segs(1:size(seg)) + segs((size(seg)+1):2*size(seg)) + segs((2*size(seg)+1):3*size(seg)) + segs((3*size(seg)+1):4*size(seg));
            ind = find(F>0);

            for i=1:size(X{ex},2)-1 % for each position in the example string
                % f should be a J length vector of every single feature
                % function - each entry should be ({string of chars}, {break
                % position}
                g(:,:,i) = calc_g(w,X{ex},i,f,usedsegs);
            end

            [~,~,y_max] = calc_u(g);
            y_maxstr = zeros(size(y_max'));
            for yctr=1:size(y_max,1)
                y_maxstr(yctr) = y_table(y_max(yctr));
            end

    %         Fy_max = zeros(4*size(seg,1),1);
    %         for i=2:min(wsize,size(X{ex},2))
    %             [Fy_max,~] = parseword( X{ex}, char(y_maxstr), i, S, Fy_max, f, size(seg,1) );
    %         end
    % 
    %         w = w + step*F;
    %         w = w - step*Fy_max;
    %         end
    %     end

            flag = 0;
            for k = 1:size(Y{ex},2)
                letterTot = letterTot + 1;
                if y_table(y_max(k)) == Y{ex}(k)
                    letterCorr = letterCorr + 1;
                else
                    flag = 1;
                end
            end

            if flag ~= 1
                wordCorr = wordCorr + 1;
            end
            wordTot = wordTot +1;
        end
    end


    string = sprintf('CROSS VALIDATION RESULTS FOR BIN %d', cross);
    disp(string);
    letterCorr/letterTot
    wordCorr/wordTot
    cross_results(cross+1,1) = letterCorr/letterTot;
    cross_results(cross+1,2) = wordCorr/wordTot;
end